﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Tomahawk.Runtime.Canvas.Sprite;
using Microsoft.Xna.Framework;

namespace Tomahawk.Runtime.Logic.UI.Components
{
    /// <summary>
    /// Reusable string render component
    /// </summary>
    public struct TextComponent
    {
        /// <summary>
        /// TextStyle name from where to read the configurable parameters
        /// </summary>
        [XmlAttribute("style")]
        public string StyleName;

        /// <summary>
        /// Text that must be rendered
        /// </summary>
        [XmlText()]
        public string Text;

        private XStyleManager.TextStyle textStyle;

        /// <summary>
        /// Gets the configured text style
        /// </summary>
        public XStyleManager.TextStyle TextStyle
        {
            get { return this.textStyle; }
        }

        private UITextSprite sprite;

        /// <summary>
        /// Initialization
        /// </summary>
        public void Initialize()
        {
            //Get the style (if none specified, use "DefaultButton")
            if ((StyleName == null) || (StyleName == "")) this.StyleName = "DefaultButton";
            textStyle = Engine.Instance.EngineConfiguration.StyleManager.GetTextStyle(this.StyleName);

            //create the font asset
            this.createFont();

            //set the default font color
            this.SetFontColor(ref this.textStyle.DefaultColor);
        }

        /// <summary>
        /// Create the font, using the asset name stored in the style configuration
        /// </summary>
        private void createFont()
        {
            if (textStyle != null)
            {
                //Create an UITextSprite
                if ((textStyle.FontAsset != null) && (textStyle.FontAsset != "") && (this.Text != null))
                {
                    sprite = new UITextSprite(textStyle.FontAsset, this.Text);
                    Engine.Instance.CanvasManager.CPR_UISprites.AddPrimitive(sprite);
                }
            }
            else
            {
                Engine.Instance.DebugConsole.Warn("Cannot create TextComponent, invalid style specified: " + this.StyleName);
            }
        }

        /// <summary>
        /// Changes the visibility of the component
        /// </summary>
        public void SetVisibility(bool value)
        {
            if (sprite != null)
            {
                sprite.Visible = value;
            }
        }

        /// <summary>
        /// Changes the position of the component
        /// </summary>
        public void SetPosition(ref Rectangle rect)
        {
            if (sprite != null)
            {
                sprite.DestinationRectangle.X = rect.X;
                sprite.DestinationRectangle.Y = rect.Y;
            }
        }

        /// <summary>
        /// Sets the font color
        /// </summary>
        public void SetFontColor(ref XStyleManager.FontColor fontColor)
        {
            if (this.sprite != null)
            {
                this.sprite.Color.R = fontColor.R;
                this.sprite.Color.B = fontColor.B;
                this.sprite.Color.G = fontColor.G;
                this.sprite.Color.A = fontColor.A;
            }
        }

        /// <summary>
        /// Returns if the component is valid (i.e. correctly initialized)
        /// </summary>
        public bool IsValid()
        {
            return (sprite != null);
        }

        /// <summary>
        /// Destruction
        /// </summary>
        public void Destroy()
        {
            if (sprite != null)
            {
                Engine.Instance.CanvasManager.CPR_UISprites.RemovePrimitive(sprite);
            }
            sprite = null;
            textStyle = null;
        }

        public bool IsReady()
        {
            bool retVal = true;
            if (sprite != null) retVal = sprite.IsReady;
            return retVal;
        }

    }

}
